package com.sxhuayuan.parking.entity;

import java.math.BigDecimal;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import com.sxhuayuan.parking.config.Platform;
import com.sxhuayuan.parking.utils.SettingUtils;
import com.sxhuayuan.parking.utils.SpringUtils;
import org.apache.commons.lang.StringUtils;

/** 车场 */
@Entity
@Table(name = "hypk_parking_lot")
public class ParkingLot extends BaseEntity {

	private static final long serialVersionUID = 4387322175739826236L;

	/** session中缓存的出口编号 */
	public static final String SESSION_ATTR_NAME = "session_lot_sn";

	/** 无感支付各通道开通状态及优先级配置 */
	public static final String PROP_PARTNERS_WEIGHT = "partnersWeight";

	/** 车场标识, 系统生成 */
	private String sn;
	/** 车场编号，第3方生成 */
	private String lotSn;
	/** 车场名字 */
	private String name;
	/** 车场位置 */
	private String address;
	/** 子商户号 */
	private String mchid;
	/** 密钥 */
	private String mchkey;
	/** 车场接口地址 */
	private String apiUrl;
	/** 车场接口令牌，对接中间件时等于车场编号 */
	private String apiUser;
	/** 车场接口密钥 */
	private String apiKey;

	/** 对应摸一个代理员 */
	private Agent agent;
	/** 每个车场对应的广告位链接 */
	private String advert;

	private String pluginId;

	/** 支付通道方式 */
	@NotNull
	private Integer payMchType; // 0 官方通道 1银行通道

	/** 服务是否可用 */
	private Boolean isEnable = false;

	/**
	 * 是否开启月卡充值
	 */
	private Boolean isMonthCardChargeEnable = false;
	/** 是否支持微信支付 */
	private Boolean isWeixinEnable = false;
	/** 是否支持支付宝 */
	private Boolean isAlipayEnable = false;
	/** 车场是否开启测试模式，开启后实际只需要支付1分钱，其他不影响 */
	private Boolean isDebug = false;

	/** 微信费率 % */
	private Float weixinRate;

	/** 支付宝费率 % */
	private Float alipayRate;

	/** 是否开启无感支付 */
	private Boolean isPartnerpayEnable = false;
	/** 是否websocket转发请求 */
	private Boolean isTcp = false;
	/** 免费时间，分钟 */
	private Integer freeMinutes;
	/** 免费离场时间分钟 */
	private Integer freeMinutesForLeaving;

	/** 发票系统配置 */
	private InvoiceSetting invoiceSetting;

	public static final Integer PAY_MCH_TYPE_OFFICIAL = 0;
	public static final Integer PAY_MCH_TYPE_BANK = 1;

	@NotBlank
	@Column(length = 32, unique = true)
	public String getSn() {
		return sn;
	}

	public void setSn(String sn) {
		this.sn = sn;
	}

	@Column(length = 32)
	public String getLotSn() {
		return lotSn;
	}

	public void setLotSn(String lotSn) {
		this.lotSn = lotSn;
	}

	@NotBlank
	@Column(length = 32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@NotBlank
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@NotBlank
	@Column(length = 32)
	public String getMchid() {
		return mchid;
	}

	public void setMchid(String mchid) {
		this.mchid = mchid;
	}

	public String getMchkey() {
		return mchkey;
	}

	public void setMchkey(String mchkey) {
		this.mchkey = mchkey;
	}

	public String getAdvert() {
		return advert;
	}

	public void setAdvert(String advert) {
		this.advert = advert;
	}

	@NotBlank
	public String getApiUrl() {
		return apiUrl;
	}

	public void setApiUrl(String apiUrl) {
		this.apiUrl = apiUrl;
	}

	@NotBlank
	@Column(length = 64)
	public String getApiUser() {
		return apiUser;
	}

	public void setApiUser(String apiUser) {
		this.apiUser = apiUser;
	}

	@NotBlank
	public String getApiKey() {
		return apiKey;
	}

	public void setApiKey(String apiKey) {
		this.apiKey = apiKey;
	}

	@NotNull
	public Integer getPayMchType() {
		return payMchType;
	}

	public void setPayMchType(Integer payMchType) {
		this.payMchType = payMchType;
	}

	@JoinColumn(name = "agent_id")
	@ManyToOne(fetch = FetchType.LAZY)
	public Agent getAgent() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	@NotEmpty
	@Column(length = 64)
	public String getPluginId() {
		return pluginId;
	}

	public void setPluginId(String pluginId) {
		this.pluginId = pluginId;
	}

	public Boolean getIsMonthCardChargeEnable() {
		return isMonthCardChargeEnable;
	}

	public void setIsMonthCardChargeEnable(Boolean isMonthCardChargeEnable) {
		this.isMonthCardChargeEnable = isMonthCardChargeEnable;
	}

	@Column(nullable = false)
	public Boolean getIsEnable() {
		return isEnable;
	}

	public void setIsEnable(Boolean isEnable) {
		this.isEnable = isEnable;
	}

	@Column(nullable = false)
	public Boolean getIsWeixinEnable() {
		return isWeixinEnable;
	}

	public void setIsWeixinEnable(Boolean isWeixinEnable) {
		this.isWeixinEnable = isWeixinEnable;
	}

	@Column(nullable = false)
	public Boolean getIsAlipayEnable() {
		return isAlipayEnable;
	}

	public void setIsAlipayEnable(Boolean isAlipayEnable) {
		this.isAlipayEnable = isAlipayEnable;
	}

	@Transient
	public String getPluginName() {
		return SpringUtils.getMessage("plugin.parking." + this.pluginId);
	}

	@Column(nullable = false, columnDefinition = "float(3,2) default '0.4'")
	public Float getWeixinRate() {
		return weixinRate;
	}

	public void setWeixinRate(Float weixinRate) {
		this.weixinRate = weixinRate;
	}

	@Column(nullable = false, columnDefinition = "float(3,2) default '0.4'")
	public Float getAlipayRate() {
		return alipayRate;
	}

	public void setAlipayRate(Float alipayRate) {
		this.alipayRate = alipayRate;
	}

	public Boolean getIsPartnerpayEnable() {
		return isPartnerpayEnable;
	}

	public void setIsPartnerpayEnable(Boolean isPartnerpayEnable) {
		this.isPartnerpayEnable = isPartnerpayEnable;
	}

	/** 预支付二维码链接 */
	@Transient
	public String getQrcodeUrl() {
		return SettingUtils.get().getSiteUrl() + "/member/park/pre/" + getSn();
	}

	public Integer getFreeMinutes() {
		return freeMinutes;
	}

	public void setFreeMinutes(Integer freeMinutes) {
		this.freeMinutes = freeMinutes;
	}

	public Integer getFreeMinutesForLeaving() {
		return freeMinutesForLeaving;
	}

	public void setFreeMinutesForLeaving(Integer freeMinutesForLeaving) {
		this.freeMinutesForLeaving = freeMinutesForLeaving;
	}

	public Boolean getIsTcp() {
		return isTcp;
	}

	public void setIsTcp(Boolean isTcp) {
		this.isTcp = isTcp;
	}


	@OneToOne
	@JoinColumn(name = "invoice_setting_id")
	public InvoiceSetting getInvoiceSetting() {
		return invoiceSetting;
	}

	public void setInvoiceSetting(InvoiceSetting invoiceSetting) {
		this.invoiceSetting = invoiceSetting;
	}

	/**
	 * 计费手续费
	 * 
	 * @return
	 */
	@Transient
	public BigDecimal calcServiceFee(Platform pf, BigDecimal money) {
		Float rate = 0f;
		if (pf == Platform.weixin) {
			rate = this.weixinRate;
		} else if (pf == Platform.alipay) {
			rate = this.alipayRate;
		}
		return SettingUtils.get().setScale(money.multiply(new BigDecimal(rate)).divide(new BigDecimal(100)));
	}

	public Boolean getIsDebug() {
		return isDebug;
	}

	public void setIsDebug(Boolean isDebug) {
		this.isDebug = isDebug;
	}
}
